module namespace common = "http://www.metaphoricalweb.com/xmlns/common";
declare namespace xs="http://www.w3.org/2001/XMLSchema";
declare namespace atom="http://www.w3.org/2005/Atom";

declare function common:get-parameters() as node() {
     let $delimiter := ";"
     let $params := if (request:get-method() = "GET") then 
          let $query-string := util:unescape-uri(request:get-query-string(),"UTF-8")
          let $parsed-query := tokenize($query-string,$delimiter)
          return <params>
          {for $parsed-query-term in $parsed-query 
                let $parse-query-name := substring-before($parsed-query-term,"=")
                let $parse-query-value := substring-after($parsed-query-term,"=")
                return <param name="{$parse-query-name}" value="{$parse-query-value}"/>
                }
          </params>          
       else 
          <params>
              {for $name in request:get-parameter-names()
                    let $parse-query-name := $name
                    let $parse-query-value := request:get-parameter($name,"")                    
                return <param name="{$parse-query-name}" value="{$parse-query-value}"/>
                }
          </params>
    return $params
    };

declare function common:get-parameter($param-name as xs:string,$default-value as xs:string) as xs:string? {
    let $params := common:get-parameters()
    let $param-node := $params/param[@name=$param-name]
    let $param-value := if ($param-node) then string($param-node/@value) else $default-value
    return  $param-value
    };
    
declare function common:get-parameter-names() as xs:string* {
    let $params := common:get-parameters()
    for $param-node in $params/param
    return string($param-node/@name)
    };

declare function common:get-by-ids($ids as xs:string*) as node()* {
    let $conf := doc("/db/xrai/common/conf/conf.xml")
    let $domain := $conf//domain[@path="/db/xrai"]
    let $topics := $domain//topic
    let $topic-set := collection(for $object in $objects return string($topic/@object-data))
    for $id in $ids 
    return $topic-set[.//*:Metadata/*:Id[string(.) = string($id)]]
};

declare function common:get-by-id($id as xs:string) as node()* {
    let $conf := doc("/db/xrai/common/conf/conf.xml")
    let $domain := $conf//domain[@path="/db/xrai"]
    let $topics := $domain//topic
    let $topic-set := collection(for $topic in $topics return string($topic/@object-data))
    return $topic-set[.//*:Metadata/*:Id[string(.) = string($id)]]
};

declare function common:get-new($topic-key as xs:string) as node()* {
    let $conf := doc("/db/xrai/common/conf/conf.xml")
    let $domain := $conf//domain[string(@id)="xrai"]
    let $new-template := doc($domain//topic[string(@id) = $topic-key]/@object-template)
    let $uuid := util:uuid()
    let $update := (
        update value $new-template//*:Metadata/*:Id with $uuid,
        update value $new-template//*:Metadata/*:DateCreated with current-dateTime(),
        update value $new-template//*:Metadata/*:DateLastModified with current-dateTime()
        ) 
    return $new-template   
   };

